//
//  HELStatisticVC.m
//  sanda
//
//  Created by 陈武琦 on 16/7/13.
//  Copyright © 2016年 陈武琦. All rights reserved.
//
static NSString *cellID = @"HELStatisticCellID";

#import "HELStatisticVC.h"
#import "HELStatisticCell.h"
#import "HELWebViewController.h"
#import "HELSuperTableView.h"
#import "UITableView+FDTemplateLayoutCell.h"

@interface HELStatisticVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;

@end

@implementation HELStatisticVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:self.title];
//    [self setNavToBlue];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELStatisticCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
    
    weakSelf_define;
    // 添加下拉刷新头部控件
    [self.tableView wsj_HeaderRefreshBlock:^{
        //请求加载数据
        [weakSelf requestData];
    }];
    
    [self requestData];
}


- (void)requestData {
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    self.tableView.loading = YES;
    NSDictionary *param = @{@"userId":userInfo.userId};
    
    defineWeakSelf;
    [[HELNetworkManager sharedMg] POST:helStatisticUrl parameters:param success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *dict in responseObject) {
                HELStatisticModel *model = [HELStatisticModel mj_objectWithKeyValues:dict];
                [arr addObject:model];
            }
            weakSelf.dataArray = [arr copy];
            [weakSelf.tableView reloadData];
        }
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }

        [weakSelf.tableView.header endRefreshing];
    } failure:^(NSString *err) {
        cwqHudRequestError(err);
        [weakSelf.tableView.header endRefreshing];
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }

    }];
}



- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kAppBg;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}
#pragma mark - <UITableViewData and UITableDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    HELStatisticModel *model = self.dataArray[indexPath.row];
//    CGFloat titleLabelH =[model.title boundingRectWithSize:CGSizeMake(kScreenWidth - 125, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
//
//   CGFloat height = titleLabelH + 70+1;
//    height = height>108 ? height:108;
    CGFloat height = [tableView fd_heightForCellWithIdentifier:cellID configuration:^(HELStatisticCell *cell) {
            [cell setModel:model];
    }];
    height = height>108 ? height:108;
    return height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELStatisticCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    HELStatisticModel *model = self.dataArray[indexPath.row];
    [cell setModel:model];
//    cell.title.text = self.dataArray[indexPath.row];
//    cell.dateTime.text = @"2016-07-01";
//    cell.fullScore.text = @"100";
//    cell.qualfied.text = @"60";
//    cell.score.text = @"100(通过)";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // Make sure the constraints have been added to this cell, since it may have just been created from scratch
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    HELStatisticModel *model = self.dataArray[indexPath.row];
    HELWebViewController *webVC = [[HELWebViewController alloc] init];
    NSString *url = [[[HELNetworkManager sharedMg] getBaseUrl] stringByAppendingString:[NSString stringWithFormat:@"%@?tblExamPaperId=%@&userId=%@",helExamDetailUrl ,model.tblExamPaperId,userInfo.userId]];
    webVC.url = [NSURL URLWithString:url];
    webVC.title = model.title;
    [self.navigationController pushViewController:webVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
